package gold.digger;

import java.util.*;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC721 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public class UF {
            // 连通分量个数
            private int count;
            // 存储一棵树
            private int[] parent;
            // 记录树的“重量”
            private int[] size;

            public UF(int n) {
                this.count = n;
                parent = new int[n];
                size = new int[n];
                for (int i = 0; i < n; i++) {
                    parent[i] = i;
                    size[i] = 1;
                }
            }

            /* 将 p 和 q 连接 */
            public void union(int p, int q) {
                int rootP = find(p);
                int rootQ = find(q);
                if (rootP == rootQ)
                    return;

                // 小树接到大树下面，较平衡
                if (size[rootP] > size[rootQ]) {
                    parent[rootQ] = rootP;
                    size[rootP] += size[rootQ];
                } else {
                    parent[rootP] = rootQ;
                    size[rootQ] += size[rootP];
                }
                count--;
            }

            /* 判断 p 和 q 是否连通 */
            public boolean connected(int p, int q) {
                int rootP = find(p);
                int rootQ = find(q);
                return rootP == rootQ;
            }

            private int find(int x) {
                while (parent[x] != x) {
                    // 进行路径压缩
                    parent[x] = parent[parent[x]];
                    x = parent[x];
                }
                return x;
            }

            /* 返回图中有多少个连通分量 */
            public int count() {
                return count;
            }
        }

        public List<List<String>> accountsMerge(List<List<String>> accounts) {
            Map<String, Integer> map = new HashMap<>();//邮箱到账户id映射
            int n = accounts.size();
            UF bc = new UF(n);
            for (int i = 0; i < n; i++) {
                List<String> account = accounts.get(i);
                for (int j = 1; j < account.size(); j++) {
                    String email = account.get(j);
                    if (map.containsKey(email)) {
                        int preId = map.get(email);
                        bc.union(preId, i);
                    }
                    map.put(email, i);//更新当前最新id
                }
            }

            //再建立一个Map，这个Map是下标和邮箱集的映射，注意，这个时候已经邮箱去重，
            // 邮箱排序，名单去重了。前两者采用TreeSet，后者通过并查集。
            Map<Integer, Set<String>> mergedIdToEmailMap = new HashMap<>();
            for (int i = 0; i < n; i++) {
                int index = bc.find(i);
                List<String> account = accounts.get(i);
                List<String> emails = account.subList(1, account.size());
                if (mergedIdToEmailMap.containsKey(index)) {
                    mergedIdToEmailMap.get(index).addAll(emails);
                } else {
                    mergedIdToEmailMap.put(index, new TreeSet<>(emails));
                }
            }

            List<List<String>> ans = new ArrayList<>();
            for (Map.Entry<Integer, Set<String>> entry : mergedIdToEmailMap.entrySet()) {
                List<String> tmp = new LinkedList<>();
                int index = entry.getKey();
                String name = accounts.get(index).get(0);
                tmp.add(name);
                tmp.addAll(entry.getValue());
                ans.add(tmp);
            }
            return ans;
        }
    }

    public void run() {
        Solution solution = new Solution();
        List<Integer> list = new ArrayList<>();
        System.out.println(solution.toString());
    }

    public static void main(String[] args) throws Exception {
        LC721 an = new LC721();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
